package com.ext.aic.handler;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ext.aic.utils.ToastyUtils;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;

import java.util.List;

public class FormValidatorHandler {

    public Validator validator;

    //step1 初始化时绑定验证器
    public <T extends Validator.ValidationListener> void bindFormValidator(T controller) {
        validator = new Validator(controller);
        validator.setValidationListener(controller);
    }

    //step2 验证失败时处理
    public void onValidationFailed(List<ValidationError> errors, Context context) {
        for (ValidationError error : errors) {
            View view = error.getView();
            String message = error.getCollatedErrorMessage(context);
            if (view instanceof EditText) {
                ((EditText) view).setError(message);
            } else if (view instanceof TextView) {
                ((TextView) view).setError(message);
            } else {
                ToastyUtils.getInstance().show(message);
            }
        }
    }

    //step3 定义验证EditText，并添加注解规则
}
